#include "MainHeader.h"

BOOL CALLBACK  SSEProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){

	
	PAINTSTRUCT ps;
	HDC hdc;
    u8 curdetail=0;
	u8 newdetail=0;
	u16 i=0;
	u32 off=0;
	u8  id=0;
	u32 j=0;
	
    sprite_entry *SE;
    char buffer[3]={0};
	switch (message) 
	{
	case WM_INITDIALOG:
		 SpriteSet.me = hWnd;
		 SpriteSet.Create();
		break;
		
		
	case WM_LBUTTONDOWN:
		
		
		break;
	case WM_COMMAND:
		switch(LOWORD(wParam)){
			 case lstSprites:
				if(HIWORD(wParam)==LBN_SELCHANGE){
					SpriteSet.GetSet();
					SpriteSet.DecodeSet();
					SpriteSet.SetInfo();
					SpriteSet.Population.SetListIndex(0);
					SendMessage(hWnd,WM_COMMAND,0x000104a7,0);
					InvalidateRect(SpriteSet.PalView,0,1);
					InvalidateRect(SpriteSet.SprView,0,1);
				}
			break;
			 case  cmdSetSprite:
				 GetWindowText(GetDlgItem(hWnd,txtSV),buffer,3);
				 sscanf(buffer,"%X",&id);
				SE  = new sprite_entry[SpriteSet.total];
                 SE[ SpriteSet.Population.GetListIndex()].sprtype=id;
				// SpriteSet.SlightChange();
				// 	SpriteSet.DecodeSet();
				// SendMessage(hWnd,WM_COMMAND,0x000104a7,0);
				   
						 for(i =0; i<SpriteSet.total;i++){
							 SE[i].sprdetail=SpriteSet.spriteset[i].sprdetail;
							 SE[i].sprtype=SpriteSet.spriteset[i].sprtype;
						 }
					
                     //we have our ID to use apply
					 //Before we do any setting lets find out if there will be enough data to hold on this else we'll error the fuck up
				
                       SpriteSet.spriteset[ SpriteSet.Population.GetListIndex()].sprtype=id;
					
					 delete[] SE;
					 SSE2=true;
					 SSE=true;
                  	SpriteSet.GetSet();
					SpriteSet.DecodeSet();
					SpriteSet.SetInfo();
					//SpriteSet.Population.SetListIndex(0);
					SendMessage(hWnd,WM_COMMAND,0x000104a7,0);
					SpriteSet.Population.SetListIndex(SpriteSet.Population.GetListIndex());
					
					InvalidateRect(SpriteSet.PalView,0,1);
					InvalidateRect(SpriteSet.SprView,0,1);
				  SSE2=false;
				  SSE=true;
				 break; 
			 case lstPopulation:
				if(HIWORD(wParam)==LBN_SELCHANGE){
					
					 SpriteSet.Ingame.id = SpriteSet.SpritePreview.id = SpriteSet.spriteset[ SpriteSet.Population.GetListIndex()].sprtype;
				     SpriteSet.SpritePreview.details= SpriteSet.Ingame.details= SpriteSet.spriteset[ SpriteSet.Population.GetListIndex()].sprdetail;
					 SpriteSet.cboDetail.SetListIndex(SpriteSet.Ingame.details);
					 SpriteSet.SetupPreview(&SpriteSet.SpritePreview);
					 sprintf(buffer,"%X",SpriteSet.spriteset[ SpriteSet.Population.GetListIndex()].sprtype);
					 SetWindowText(GetDlgItem(hWnd,txtSV),buffer);
					 SpriteSet.SetupPreview(&SpriteSet.Ingame);
				}
				 break;
			 case cboSlot:
				 if(HIWORD(wParam)==CBN_SELCHANGE){
					 //Change this sprites slot :O could it cause problems? We'll see!
					 curdetail=SpriteSet.spriteset[ SpriteSet.Population.GetListIndex()].sprdetail;
					 
					 for(i=0;i<SpriteSet.total;i++){
						 if((SpriteSet.spriteset[i].sprdetail==SpriteSet.cboDetail.GetListIndex())){//Look for the new detail from the combo 
                            id=1;
							 break;  
                              
						 } else {

							 id=0;
						 }
                         
					 }
if(id)SpriteSet.spriteset[i].sprdetail=curdetail;
 SpriteSet.spriteset[ SpriteSet.Population.GetListIndex()].sprdetail=SpriteSet.cboDetail.GetListIndex();
					 
				 SSE2=true;
				 SSE=true;
                  	SpriteSet.GetSet();
					SpriteSet.DecodeSet();
					SpriteSet.SetInfo();
					//SpriteSet.Population.SetListIndex(0);
					SpriteSet.Population.SetListIndex(SpriteSet.Population.GetListIndex());
					SendMessage(hWnd,WM_COMMAND,0x000104a7,0);
					InvalidateRect(SpriteSet.PalView,0,1);
					InvalidateRect(SpriteSet.SprView,0,1);
				  SSE2=false;
				  SSE=true;
				 
				 }


				 break;
			case cmdSaveSet:
                //Just bare min saving 
				if(GameEngine->isZM()){
					
					fseek(GBA.ROM, (SpriteSet.SpriteSets.GetListIndex()* 4) + 0x75F31C, SEEK_SET);
					fread(&off, sizeof(long), 1, GBA.ROM);
					fseek(GBA.ROM, off-0x8000000, SEEK_SET);



				}else if(GameEngine->isMF()){
					fseek(GBA.ROM, SpriteSet.SpriteSets.GetListIndex()*4 + 0x79ADD8, SEEK_SET);
					fread(&off, sizeof(long), 1, GBA.ROM);
					fseek(GBA.ROM,off-0x8000000, SEEK_SET);
					

				}

					for( i=0; i< SpriteSet.total; i++) {
						fwrite(&SpriteSet.spriteset[i].sprtype, 1, 1, GBA.ROM);
						fwrite(&SpriteSet.spriteset[i].sprdetail, 1, 1, GBA.ROM);
					}
					SendMessage(hWnd,WM_COMMAND,HIWORD(LBN_SELCHANGE)|LOWORD(lstSprites),0);
			        SendMessage(Main,WM_COMMAND,0x00010408,0);
				 break;
			case cmdSavePal:
				SSE=true;
             SavePal(SpriteSet.palinfo,SpriteSet.spriteset,SpriteSet.pal );
             GBA.Reopen();
                SSE=false;
				SendMessage(Main,WM_COMMAND,0x00010408,0);
				break;
			case WM_DESTROY:
				ShowWindow(hWnd,SW_HIDE);
				break;	
	
		}
		break;
	case WM_VSCROLL:	// exact same idea, but V scroll instead of H scroll
		
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		
	SpriteSet.SpritePreview.PreviewSprite.Blit(hdc, 206,245,82,128,0,0);
	//SpriteSet.Ingame.PreviewSprite.Blit(hdc,296 ,245,82,128,0,0);
	
       
	   	EndPaint(hWnd, &ps);
		break;
		case WM_SHOWWINDOW:
		 // LoadCurSprite();	
			break;
			
			
	}
	
//return DefWindowProc(hWnd, message, wParam, lParam);
	return 0;
}

int cSSE::Create(){
    char tehbuf[256]={0};
    int i=0;
	SpriteSets.Init(GetDlgItem(me,lstSprites));
    Population.Init(GetDlgItem(me,lstPopulation));
	cboDetail.Init(GetDlgItem(me,cboSlot));
	cboDetail.Clear();
	SpriteSets.Clear();
	Population.Clear();
    for(i = 0;i < 256;i++){
        sprintf(tehbuf,"%X", i );
		SpriteSets.Additem(tehbuf);
		if(16>i)Population.Additem(tehbuf);
        if(8>i)cboDetail.Additem(tehbuf);
	}
	CreateSpriteWindow();
    CreatePalWindow();
    SpriteSets.SetListIndex(0);
	return 0;
}






int cSSE::GetSet(){
	char tehbuf[256]={0};
	int i = 0;
	total =0;
	if(GameEngine->isZM()){
		SSE=true;

		LoadSet(gfxinfo,palinfo, spriteset,(u8)SpriteSets.GetListIndex());
        SSE=false;
	}else if(GameEngine->isMF()){
	    SSE=true;
        MFLoadSet(gfxinfo,palinfo, spriteset,(u8)SpriteSets.GetListIndex());
	    SSE=false;
	}
   Population.Clear();
    for(i = 0;i < 	SpriteSet.total;i++){
        sprintf(tehbuf,"%X", i );
	
		Population.Additem(tehbuf);

	}
	return 0;
}

int cSSE::DecodeSet(){
	int i =0;
    SSE=true;
	memset(pal,0,sizeof(pal));
	memset(GBASprPal,0,sizeof(GBASprPal));
	LoadPal(palinfo,spriteset,pal );

	LoadSpriteToMem(gfxinfo,spriteset,graphics,&SprGraphics);	
    SSE=false;
	Tiles.Create(512,512);
	Tiles.SetPalette(pal);
	for(i= 512; i<1024;i++){
		Tiles.Draw(SprGraphics,((i-512) % 32)*8,((i-512)/32)*8,0x8000+i);
	}

return 0;
}

void DrawPal(HDC hdc,long* palette){//Is called my WM_Paint for the palette window

	DrawPal(hdc,palette,0, 0, 128);
}
int DrawPal(HDC hdc,long* palette,int X, int Y, int palcol){

	int i =0;
	HBRUSH curbrush=NULL;
	RECT myrect={0,0,0,0};
	 for(i = 0; i<256;i++){
  
	curbrush = CreateSolidBrush(0);
	myrect.left = ((i) % 16)*16;
	myrect.top = ((i)/16)*16; 
	myrect.right = myrect.left +16; 
	myrect.bottom = myrect.top + 16;
	FillRect(hdc, &myrect,curbrush);
	DeleteObject(curbrush);

	}
    for(i = palcol; i<256;i++){
  	curbrush = CreateSolidBrush(palette[i]);
	myrect.left = ((i-palcol) % 16)*16;
	myrect.top = ((i-palcol)/16)*16; 
	myrect.right = myrect.left +16 ; 
	myrect.bottom = myrect.top + 16;
	FillRect(hdc, &myrect,curbrush);
	DeleteObject(curbrush);

	}
	return 0;

}

int cSSE::CreatePalWindow(){
WNDCLASSEX garh;
	memset(&garh,0,sizeof(garh));
	garh.cbSize = sizeof(garh);		   // size of structure 
	garh.style = CS_HREDRAW | 
		CS_VREDRAW; 				   // redraw if size changes 
	garh.lpfnWndProc = &SSEPalProc;	  // points to window procedure 
	garh.cbClsExtra = NULL;				// no extra class memory 
	garh.cbWndExtra =NULL; 			   // no extra window memory 
	garh.hInstance = hGlobal;		   // handle to instance 
	garh.hIcon = NULL; 			 // predefined app. icon 
	garh.hCursor = NULL;				// predefined arrow 
	garh.hbrBackground = (HBRUSH)GetStockObject(15);				   // white background brush 
	garh.lpszMenuName =  NULL;    // name of menu resource 
	garh.lpszClassName = "SSEPal";  // name of window class 
	
	
	// Register the window class. 
	RegisterClassEx(&garh);
	
	
	//Create the Window
	
	
	
 PalView   = CreateWindowEx(NULL,"SSEPal",NULL,WS_VISIBLE| WS_CHILD,5,11,256,128,GetDlgItem(me,fraPal),0,hGlobal,0);


	return 0;
}


LRESULT CALLBACK SSEPalProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
	HDC hdc;
	PAINTSTRUCT ps;
	int buf=0;
	int i=0;
	char tpal[256]={0};
	
	switch (message) 
	{
	case WM_PAINT:
		
		hdc = BeginPaint(hWnd, &ps);
		
    DrawPal(hdc, SpriteSet.pal);
		
		
		
		EndPaint(hWnd, &ps);
		break;
	case WM_RBUTTONDOWN:
	//	sprintf(tpal,"%d",(GetX(lParam)/16)+(GetY(lParam)/16)*16);

		//MessageBox(hWnd,tpal,NULL,MB_OK);
		break; 
		
		
	
	}
	
	return DefWindowProc(hWnd, message, wParam, lParam);
}


int cSSE::CreateSpriteWindow(){
WNDCLASSEX blahf;
	memset(&blahf,0,sizeof(blahf));
	blahf.cbSize = sizeof(blahf);		   // size of structure 
	blahf.style = CS_HREDRAW | 
		CS_VREDRAW; 				   // redraw if size changes 
	blahf.lpfnWndProc = &SSESpriteProc;	  // points to window procedure 
	blahf.cbClsExtra = NULL;				// no extra class memory 
	blahf.cbWndExtra =NULL; 			   // no extra window memory 
	blahf.hInstance = hGlobal;		   // handle to instance 
	blahf.hIcon = NULL; 			 // predefined app. icon 
	blahf.hCursor = NULL;				// predefined arrow 
	blahf.hbrBackground = (HBRUSH)GetStockObject(15);				   // white background brush 
	blahf.lpszMenuName =  NULL;    // name of menu resource 
	blahf.lpszClassName = "cSSEs";  // name of window class 
	
	
	// Register the window class. 
	RegisterClassEx(&blahf);
	
	
	//Create the Window
	
	
	
	SprView= CreateWindowEx(NULL,"cSSEs",NULL,WS_VISIBLE| WS_CHILD,5,11,256,130,GetDlgItem(me,fraTiles),0,hGlobal,0);


	return 0;
}


LRESULT CALLBACK SSESpriteProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
	HDC hdc;
	PAINTSTRUCT ps;
    
	switch (message) 
	{
	case WM_PAINT:
		
		hdc = BeginPaint(hWnd, &ps);
		
    	SpriteSet.Tiles.Blit(hdc,0,0,512,512,0,0);
		
		
		
		EndPaint(hWnd, &ps);
		break;
	
	
		
		
		break;
	}
	
	return DefWindowProc(hWnd, message, wParam, lParam);
}

int cSSE::SetInfo(){
	char buffer[2048]={0};
    u16 math=0;
	math=0x4000-usedGFX;
    sprintf(buffer,"Sprite Graphic Data:\nThis set has %X bytes free",math);
	SetWindowText(GetDlgItem(me,lblSpriteInfo),buffer);
	math = 8-((usedPAL/0x10));
    sprintf(buffer,"Sprite Palette Data:\nThis set has %X palettes free",math);
	SetWindowText(GetDlgItem(me,lblPaletteInfo),buffer);

	return 0;

}


int cSSE::SetupPreview(SprGBuf* SprG){
//Sets up the preview control, will be called alot
  long GFXPnt=0;
long PalPnt=0;

long addybuf=0;
long PalPos=0;
long size=0;

int i=0;
int ii=0;
u8 decompbuf[32687]= {0};
SprG->PreviewSprite.Clear();
InvalidateRect(me,0,1);
if(SpriteOAM[SprG->id]==0) return 0;
	memset(&SprG->PreRAM[0x4000], 0, 0x4000);
CompHeader thiscompheader;

	u16 transferpal[256]={0};

	int  X=0;
	long off=0;
	int x =0;
	memset(SprPal,0,sizeof(SprPal));
//	memset(&GBASprPal[128],0,sizeof(GBASprPal)-128);
//    memset(&SprPal,0,sizeof(SprPal));

	if(GameEngine->isZM()){
		GFXPnt= sprgfxpnt + (SprG->id-0x10) * 4;
		
		fseek(GBA.ROM, sprgfxpnt + (SprG->id -0x10 ) * 4, SEEK_SET);
		fread(&off, 4, 1, GBA.ROM);
		fseek(GBA.ROM, off-0x8000000, SEEK_SET);
		fread(&thiscompheader.check_ten, 1, 1, GBA.ROM);       //Check byte should be 0x10 for lz
		fread(&thiscompheader.l3, 1, 1, GBA.ROM);
		fread(&thiscompheader.l2, 1, 1, GBA.ROM);
		fread(&thiscompheader.l1, 1, 1, GBA.ROM);

		PalPnt = sprpalpnt + (SprG->id-0x10) * 4;
	
		SprG->palsize= (thiscompheader.l2 & 0xF8) * 2;

	
	}else if(GameEngine->isMF()){
        GFXPnt= 0x79A5D8 + (SprG->id-0x10) * 4;
		


		PalPnt= 0x79A8D4 + (SprG->id-0x10) * 4;
       
		SprG->palsize=  ((MFSprSize[(SprG->id-0x10)<<1]/2048)*16);


	}
	
    
       
//		fseek(GBA.ROM, PalPnt , SEEK_SET);
//		fread(&addybuf, 4, 1, GBA.ROM);
//		fseek(GBA.ROM, addybuf-0x8000000, SEEK_SET);
//		fread(&transferpal, 1, (	palsize)*2, GBA.ROM);
         
//	if(paltransfer[x][1] == 0) continue;
//	 memcpy(&GBASprPal[128], &transferpal, palsize*2);
           
//	GBA.DecodePal(GBASprPal, SprG->PreviewPal, 16, 0);
	// }else if(SprG->details>0){
    //   memcpy(&SprG->PreviewPal[0],&SpriteSet.pal[0],512);

//	}

//	memcpy(&SprG->PreviewPal[128], &SpriteSet.pal[(0x8+SprG->details)*16], palsize*2);
//	for(i = 0;i<palsize;i++)SprG->PreviewPal[128+i]=SpriteSet.pal[128+i];

//memcpy(&SprG->PreviewPal[128],&SpriteSet.pal[(8+SprG->details)*16],palsize*2);
for(i=0;i<SprG->palsize;i++)SprG->PreviewPal[128+i]=SpriteSet.pal[(8+SprG->details)*16 + i];
//	memcpy(SprG->PreviewPal,&SpriteSet.pal[128],128*4);
		switch(GameEngine->isZM()){
				case 0:
					fseek(GBA.ROM, GFXPnt, SEEK_SET);
					fread(&addybuf, 4, 1, GBA.ROM);
					fseek(GBA.ROM, addybuf-0x8000000, SEEK_SET);
					fread(GBA.CompBuffer, 1, sizeof(GBA.CompBuffer), GBA.ROM);
					size = GBA.LZ77UnComp(GBA.CompBuffer, decompbuf);
					memcpy(&SprG->PreRAM[0x4000], &decompbuf, size);
				break; 
				case 1:
                    size = MFSprSize[(SprG->id-0x10)<<1];
					fseek(GBA.ROM, GFXPnt, SEEK_SET);
					fread(&addybuf, 4, 1, GBA.ROM);
					fseek(GBA.ROM, addybuf-0x8000000, SEEK_SET);
				    fread(&SprG->PreRAM[0x4000], 1, size, GBA.ROM);
				break;
		}
	
SSE=true;
//memcpy(&SprG->PreRAM[0x4000],&SpriteSet.graphics[0x4000+(SprG->id*128)],size);
//fp = fopen("C:\\Documents and Settings\\Matt\\Desktop\\blah3.gba","r+");
//fwrite(SprG->PreRAM,1,0x8000,fp);
//fclose(fp);
//&SprG->PreRAM=&SpriteSet.graphics;
SprG->PreviewSprite.SetPalette(SprG->PreviewPal);
memcpy(SprG->PreRAM,SpriteSet.graphics,0x8000);
SprG->Tiles.Load(SprG->PreRAM,1024);
DecodeSprite(SprG,SpriteOAM[SprG->id]);
	DrawSSprite(SprG);
	SSE=false;
	InvalidateRect(me,0,1);


return 0;
}


int DrawSSprite( SprGBuf* SpriteDetails) {
	int width=0;
	int height=0;
	int i =0;
	int Tile=0;
	int bTile=0;
	int Pal=0;
	int fw=0;
	int fh=0;
	int ty=0;
	int tx=0;
	int sx=0;
	int sy=0;
	int xFlip=0;
	int yFlip=0;
	RECT PartSize[128];
	const u8 objSizes[3][4][2]=
	{
		{ { 8, 8}, {16,16}, {32,32}, {64,64} }, 
		{ {16, 8}, {32, 8}, {32,16}, {64,32} },
		{ { 8,16}, { 8,32}, {16,32}, {32,64} },
	};
	
	Image OamBuffer; 
	


	sx = 0;
	sy = 0;

	tx = 0;
	ty = 0;
    OamBuffer.Create(512, 512);

	OamBuffer.SetPalette(SpriteDetails->PreviewPal);
  



	//Draw reverse order
 for(i = SpriteDetails->maxparts-1; i>=0; i--) {
	
	//for(i = 0; i<SpriteDetails->maxparts; i++) {
	    

		if ( SpriteDetails->OAM[i].deOAM.ObjShape == 3)
			continue;

		sx = ( SpriteDetails->OAM[i].enOAM.OAM1 & 511);
		sy = ( SpriteDetails->OAM[i].enOAM.OAM0 & 255);
		if (sy & 0x80) 
			sy = sy - 256; //check for the negative
		if (sx &  0x100)  
			sx = sx - 511; //same here
         
		sx = sx + 128;
		sy = sy + 128;
		//Set up the basic tile
    
      
	
	
		//Now switch on this and find the sprite width and height.
	u16 Pal = ((SpriteDetails->OAM[i].enOAM.OAM2&0xF000)); 
		    //(+(SpriteDetails->details)* 0x1000);

		//MsgBox "Out of Memory Bounds": Exit Function
		Tile=Pal + (SpriteDetails->details  * 64) + 	(SpriteDetails->OAM[i].enOAM.OAM2 & 0x3FF);

		//u16 Pal = ((SpriteDetails->OAM[i].enOAM.OAM2&0xF000));

		//MsgBox "Out of Memory Bounds": Exit Function
		//Tile=Pal +  	(SpriteDetails->OAM[i].enOAM.OAM2 & 0x3FF);

		width = objSizes[ SpriteDetails->OAM[i].enOAM.OAM0>>14][ SpriteDetails->OAM[i].enOAM.OAM1>>14][0];
		height= objSizes[ SpriteDetails->OAM[i].enOAM.OAM0>>14][ SpriteDetails->OAM[i].enOAM.OAM1>>14][1];
        PartSize[SpriteDetails->maxparts-i-1].left =sx;
		PartSize[SpriteDetails->maxparts-i-1].top =sy;
        PartSize[SpriteDetails->maxparts-i-1].right =width;
		PartSize[SpriteDetails->maxparts-i-1].bottom =height;
		xFlip= yFlip= fh= fw= 0;

		if( SpriteDetails->OAM[i].enOAM.OAM1 & 0x1000) {
			//formula goes here;
			xFlip= width/8-1; //if width = 16 y
			fw = 0x400;
		} 
		if( SpriteDetails->OAM[i].enOAM.OAM1 & 0x2000 ) {
			yFlip = height/8-1;
			fh = 0x800;
		} 

		for(ty=0; ty<height/8; ty++) {

			for(tx=0; tx<width/8; tx++) {

				OamBuffer.Draw(SpriteDetails->Tiles, sx + (tx^xFlip)*8,
					sy + ((ty)^yFlip)*8, Tile+tx+(ty*32)+fh+fw);
				
			}
		}
		

/*     
		HDC hdc= OamBuffer.DC();
		HPEN pen;
		pen= CreatePen(PS_SOLID, 1, i==0 ? RGB(255,0,0) : RGB(0, 255, 255));

		HPEN oldPen= (HPEN)SelectObject(hdc, pen);
		HBRUSH oldBrush= (HBRUSH)SelectObject(hdc, GetStockObject(HOLLOW_BRUSH));
		Rectangle(hdc, sx, sy, sx+width, sy+width);
		SelectObject(hdc, oldBrush);		
		SelectObject(hdc, oldPen);*/
	}

	//Determine i
	SpriteDetails->Borders.left =PartSize[0].left;
	SpriteDetails->Borders.top =PartSize[0].top;
    SpriteDetails->Borders.right =PartSize[0].left+PartSize[0].right;
	SpriteDetails->Borders.bottom =PartSize[0].top+PartSize[0].bottom;

	for(i = 1; i<SpriteDetails->maxparts; i++) {
		 //Check for top coord 
		 //0 = Starting X
		 //1 = Startiny Y
		 //2 = Width
		 //3 = Height
		if(PartSize[i].left<SpriteDetails->Borders.left) 
			SpriteDetails->Borders.left = PartSize[i].left;
		if(PartSize[i].top<SpriteDetails->Borders.top) 
			SpriteDetails->Borders.top = PartSize[i].top;

		SpriteDetails->Borders.right= 
			max(SpriteDetails->Borders.right, PartSize[i].left+PartSize[i].right);
		SpriteDetails->Borders.bottom= 
			max(SpriteDetails->Borders.bottom, PartSize[i].top+PartSize[i].bottom);

	}
	
	SpriteDetails->PreviewSprite.Create(SpriteDetails->Borders.right-SpriteDetails->Borders.left,	
		SpriteDetails->Borders.bottom-SpriteDetails->Borders.top);

	SpriteDetails->PreviewSprite.SetPalette(SpriteDetails->PreviewPal);   //(SpriteSet.pal);
/*	BitBlt(SpritePics[cursprite].DC(), 0, 0, 
		OverallSize[cursprite].right-OverallSize[cursprite].left,
		OverallSize[cursprite].bottom-OverallSize[cursprite].top,
		OamBuffer.DC(), OverallSize[cursprite].left, OverallSize[cursprite].top, 
		SRCCOPY);*/
	OamBuffer.Blit(SpriteDetails->PreviewSprite.DC(), 0, 0, 
		SpriteDetails->Borders.right-SpriteDetails->Borders.left,	
		SpriteDetails->Borders.bottom-SpriteDetails->Borders.top,SpriteDetails->Borders.left,SpriteDetails->Borders.top);

	


	return 0;
}

int cSSE::SlightChange(){
	char tehbuf[256]={0};
	int i = 0;
	total =0;
	if(GameEngine->isZM()){
		SSE2=true;

		LoadSet(gfxinfo,palinfo, spriteset,(u8)SpriteSets.GetListIndex());

        SSE2=false;
	}else if(GameEngine->isMF()){
	    SSE2=true;
        MFLoadSet(gfxinfo,palinfo, spriteset,(u8)SpriteSets.GetListIndex());
	    SSE2=false;
	}
 
	return 0;
}

int CheckTheSize(sprite_entry* SE){
long* GFX=0;
long* Pal=0;
u32   i=0;
u32   off=0;
u32   j=0;
u8 id=0;



	                     GFX = new long[SpriteSet.total];
						 Pal = new long[SpriteSet.total];
						for(i=0;i<SpriteSet.total;i++) GFX[i] = Pal[i]=0;
						 off=0;
					 if(GameEngine->isZM()){
					 
					
						 GetZMSetSZ(GFX,Pal, SE);
						 //Calculate Size
						 for(i=0;i<SpriteSet.total;i++)    
							 j+=GFX[i];
					 
					  if(j<=0x4000){
                       
                           off++;
							}	
						j=0;
                        for(i =0;i < SpriteSet.total;i++) j+=Pal[i];
						if(j<256){

                          off++;

						}
					 }else if(GameEngine->isMF()){
         
                      GetMFSetSZ(GFX,Pal, SE);
					  	 for(i=0;i<SpriteSet.total;i++)    j+=GFX[i];
					 
					  if(j<0x4000){
                       
                           off++;
							}	
						j=0;
                        for(i =0;i < SpriteSet.total;i++) j+=Pal[i];
						if(j<256){

                          off++;

						}
					 } 
					     delete[] GFX;
						 delete[] Pal;
					/*
					 if(off==2){     
					    return 1;
					 } else {
						 MessageBox(0,"The current sprite/details do not work\nReverting to working","Error",MB_OK);
					 
					 }*/
					 return 0;

}
